<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>

<body id="optPage">
<form class="layui-form layui-form-pane" id="commentForm" style="padding-bottom: 500px">
    <div class="page-title"><div class="page-title-con"><span id="flowsetname"></span><div class="move_title"></div><?php include "../public/subturn.php";?></div></div>
    <div class="main-container">
        <div class="block-generic">
            <?php include "../public/substt.php";?>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>项目信息</span></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label"><text>*</text>项目名称</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="enginid" class="layui-input enginid" onclick="openPage('../public/choose_engin.php?dom=enginid&page=engin_budget','80%','80%')">
                            <div class="choose_div" id="enginid" key="1"></div>
                            <span class="choose_one" onclick="openPage('../public/choose_engin.php?dom=enginid&page=engin_budget','80%','80%')"><i class="layui-icon layui-icon-search"></i></span>
                        </div>
                    </div>
                </div>
            </div>


            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>预算信息</span></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">预算编号</label>
                        <div class="layui-input-block">
                            <input type="text" id="number" placeholder="请输入" disabled autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>预算主题</label>
                        <div class="layui-input-block">
                            <input type="text" name="title" id="title" placeholder="请输入" clearable autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label">预算总金额(元)</label>
                        <div class="layui-input-block">
                            <input type="text" name="money" id="money" readonly placeholder="根据明细自动计算" autocomplete="off" class="layui-input moneyinput disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">其中材料费预算(元)</label>
                        <div class="layui-input-block">
                            <input type="text" name="caifee" id="caifee" readonly placeholder="根据明细自动计算" autocomplete="off" class="layui-input moneyinput disabled">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">材料费占比(%)</label>
                        <div class="layui-input-block">
                            <input type="text" name="cairate" id="cairate" readonly placeholder="根据明细自动计算" autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">其中人工费预算(元)</label>
                        <div class="layui-input-block">
                            <input type="text" name="renfee" id="renfee" readonly placeholder="根据明细自动计算" autocomplete="off" class="layui-input moneyinput disabled">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">人工费占比(%)</label>
                        <div class="layui-input-block">
                            <input type="text" name="renrate" id="renrate" readonly placeholder="根据明细自动计算" autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">其中机械设备费预算(元)</label>
                        <div class="layui-input-block">
                            <input type="text" name="jifee" id="jifee" readonly placeholder="根据明细自动计算" autocomplete="off" class="layui-input moneyinput disabled">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">机械设备费占比(%)</label>
                        <div class="layui-input-block">
                            <input type="text" name="jirate" id="jirate" readonly placeholder="根据明细自动计算" autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">其中专业分包费预算(元)</label>
                        <div class="layui-input-block">
                            <input type="text" name="fenfee" id="fenfee" readonly placeholder="根据明细自动计算" autocomplete="off" class="layui-input moneyinput disabled">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">专业分包费占比(%)</label>
                        <div class="layui-input-block">
                            <input type="text" name="fenrate" id="fenrate" readonly placeholder="根据明细自动计算" autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">其中其他费预算(元)</label>
                        <div class="layui-input-block">
                            <input type="text" name="qifee" id="qifee" readonly placeholder="根据明细自动计算" autocomplete="off" class="layui-input moneyinput disabled">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">其他费占比(%)</label>
                        <div class="layui-input-block">
                            <input type="text" name="qirate" id="qirate" readonly placeholder="根据明细自动计算" autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">编制日期</label>
                        <div class="layui-input-block">
                            <input type="text" name="biandt" id="biandt" placeholder="请选择" clearable  autocomplete="off" class="layui-input bdate_ymd">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">编制人</label>
                        <div class="layui-input-block">
                            <input type="text" name="bianer" id="bianer" placeholder="请输入" clearable autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item height_auto">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label label_textarea">备注</label>
                        <div class="layui-input-block">
                            <textarea id="explain" name="explain" placeholder="请输入" class="layui-textarea"></textarea>
                        </div>
                    </div>
                </div>
            </div>


            <div class="layui-form-item height_auto">
                <div class="short_table_list" key="1">
                    <div class="opt-btn-group">
                        <div><span>预算明细</span></div>
                        <div>
                            <button type="button" onclick="ctrlv_data_open()" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn">粘贴新增</button>
                            <button type="button" onclick="import_list()" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn">Excel导入</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_copy_row">复制行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_add_row">插入行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_row">删除行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_all">删除全部</button>
                        </div>
                    </div>
                    <table class="layui-table" lay-size="sm">
                        <thead>
                        <tr>
                            <th width="45">序号</th>
                            <th><text>*</text>预算分类</th>
                            <th><text>*</text>名称及规格</th>
                            <th><text>*</text>单位</th>
                            <th><text>*</text>预算数量</th>
                            <th><text>*</text>预算单价(元)</th>
                            <th>预算金额(元)</th>
                            <th>预算说明</th>
                        </tr>
                        </thead>
                        <tbody class="short_table_tbody" id="list_list"></tbody>
                    </table>
                    <div class="listerror">提示：<span></span></div>
                </div>
            </div>

            <?php include '../public/flowqita.php'?>
            <?php include '../public/upinfo.php'?>
        </div>
    </div>
</form>
<script>
    $("#commentForm").validate({
        rules: {
            title: {required: true},
        },
        messages:{
            title: {required: "请输入"},
        }
    });

    function import_list() {
        openPage('../public/import.php?reveal=engin_budget@importdata', '770px', '490px');
    }


    var pub_budtype;
    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;
        render_input_attr();

        facade.ajax({type: 'get', url: ask_flowdata_add_engin_budget}).done(function(res){
            if (res.code === 2000) {
                var data = res.data;
                chu_flow_qian(data);

                $("#number").val(data.number);
                $("#bianer").val(data.bianer);
                $("#biandt").val(data.biandt);



                pub_budtype = res.data.budtype;
                var budtype = res.data.budtype;
                var listAddStr = '';
                listAddStr += '<tr>';
                listAddStr += '<td style="width: 45px">1</td>';
                listAddStr += '<td><select class="layui-select budtype" lay-filter="budtype">';
                listAddStr += '<option value="">请选择</option>';
                for (var mmm in budtype) {
                    listAddStr += '<option value="'+mmm+'">'+budtype[mmm]+'</option>';
                }
                listAddStr += '</select></td>';
                listAddStr += '<td><input type="text" class="layui-input name"></td>';
                listAddStr += '<td><input type="text" class="layui-input unit"></td>';
                listAddStr += '<td><input type="number" class="layui-input num"></td>';
                listAddStr += '<td><input type="number" class="layui-input price"></td>';
                listAddStr += '<td><input type="number" class="layui-input money disabled" disabled></td>';
                listAddStr += '<td><input type="text" class="layui-input explain"></td>';
                listAddStr += '</tr>';
                short_table_opt_func(listAddStr, 1);



                public_upload_file_load('upload_files', res.data.files);
                render_date();
                render_from();
                initParseMoney();
            }
        });


        form.on('select(budtype)', function(data){
            jisuan_money_all()
        });

        form.on('submit(add)', function(data){
            data.field.isturn = $(this).attr('key') || 0;
            data.field.flowrelas = get_choose_val('flowrelas');
            data.field.advaners = tan_tags_getvalue("#advaners", 'user');
            data.field.nexter = tan_tags_getvalue("#nexter", 'user');
            parseMoney(data);

            data.field.enginid = get_choose_val('enginid');
            if(data.field.isturn > 0){
                var validRes = $("#commentForm").valid();
                formValid('enginid');
                if(data.field.enginid == undefined || !data.field.enginid){
                    validRes = false;
                }
                if(!validRes){return false;}
            }


            var list = [''];
            try {
                $('#list_list tr').each(function (i) {
                    var budtype = $(this).find('.budtype').val();
                    var name = $(this).find('.name').val();
                    var unit = $(this).find('.unit').val();
                    var num = $(this).find('.num').val();
                    var price = $(this).find('.price').val();
                    var money = $(this).find('.money').val();
                    var explain = $(this).find('.explain').val();

                    var index = i + 1;
                    if(data.field.isturn > 0){
                        if(!budtype){
                            throw new Error('第'+index+'行，请选择预算分类');
                        }
                        if(!name){
                            throw new Error('第'+index+'行，请输入名称及规格');
                        }
                        if(!unit){
                            throw new Error('第'+index+'行，请输入单位');
                        }
                        if(num==''){
                            throw new Error('第'+index+'行，请输入预算数量');
                        }
                        if(price==''){
                            throw new Error('第'+index+'行，请输入预算单价');
                        }
                    }

                    list.push({
                        budtype:budtype,
                        name:name,
                        unit:unit,
                        num:num,
                        price:price,
                        money:money,
                        explain:explain,
                    });
                });
            } catch(e) {
                $(".listerror").find('span').text(e.message);
                $(".listerror").show();
                return false;
            }
            $(".listerror").hide();
            data.field.list = list;


            data.field.files = public_upload_file_value('upload_files');
            data = parseMoney(data);
            public_flow_submit(data.field, ask_flowdata_add_engin_budget, ask_flowdata_edit_engin_budget, $(this).attr('but'));
            return false;
        });

    });






    $('#list_list').on('input propertychange','.num, .price',function () {
        jisuan_money_all();
    });
    function jisuan_money_all() {
        var allmoney = 0;
        var allcaifee = 0, allcairate = 0;
        var allrenfee = 0, allrenrate = 0;
        var alljifee = 0, alljirate = 0;
        var allfenfee = 0, allfenrate = 0;
        var allqifee = 0, allqirate = 0;


        $('#list_list tr').each(function (i) {
            var num = parseFloat($(this).find('.num').val()) || 0;
            var price = parseFloat($(this).find('.price').val()) || 0;

            var money = parseFloat(num) * parseFloat(price);
            $(this).find('.money').val(parseFloat(money).toFixed(2))

            allmoney = parseFloat(allmoney) + parseFloat(money);

            var budtype = $(this).find('.budtype').val() || '';
            if(budtype == 'cai'){
                allcaifee = parseFloat(allcaifee) + parseFloat(money);
            }
            if(budtype == 'ren'){
                allrenfee = parseFloat(allrenfee) + parseFloat(money);
            }
            if(budtype == 'ji'){
                alljifee = parseFloat(alljifee) + parseFloat(money);
            }
            if(budtype == 'fen'){
                allfenfee = parseFloat(allfenfee) + parseFloat(money);
            }
            if(budtype == 'qi'){
                allqifee = parseFloat(allqifee) + parseFloat(money);
            }
        });


        $("#money").val(parseFormatNum(allmoney, 2));

        allcairate = allcaifee/allmoney*100;
        $("#cairate").val(parseFormatNum(allcairate, 2));
        $("#caifee").val(parseFormatNum(allcaifee, 2));

        allrenrate = allrenfee/allmoney*100;
        $("#renrate").val(parseFormatNum(allrenrate, 2));
        $("#renfee").val(parseFormatNum(allrenfee, 2));

        alljirate = alljifee/allmoney*100;
        $("#jirate").val(parseFormatNum(alljirate, 2));
        $("#jifee").val(parseFormatNum(alljifee, 2));

        allfenrate = allfenfee/allmoney*100;
        $("#fenrate").val(parseFormatNum(allfenrate, 2));
        $("#fenfee").val(parseFormatNum(allfenfee, 2));

        allqirate = allqifee/allmoney*100;
        $("#qirate").val(parseFormatNum(allqirate, 2));
        $("#qifee").val(parseFormatNum(allqifee, 2));

        initParseMoney();
    }

    function update_refresh_row_func() {
        render_date();
        render_from();
        jisuan_money_all();
        $.each($("#list_list").children('tr'),function (key,val) {
            $(this).children('td:first-child').html(key+1);
        });

        setTimeout(function () {
            $(".short_table_list table tbody tr").click(function () {
                $(".short_table_list table tbody tr").removeClass('line-row');
                $(".short_table_list table tbody tr").attr('sle','');
                $(this).addClass('line-row');
                $(this).attr('sle','sle');
            });
        },1000);
    }






    var ctrlv_field_name = [
        {field: 'budtype', name:'预算分类'},
        {field: 'name', name:'名称及规格'},
        {field: 'unit', name:'单位'},
        {field: 'num', name:'预算数量(元)',type:'number'},
        {field: 'price', name:'预算单价(元)',type:'number'},
        {field: 'explain', name:'预算说明'},
    ];


    function ctrlv_data_show(list) {
        if(list.length > 0){
            var nums = $("#list_list tr").length;
            for (var key in list){
                var str = '';
                nums++;
                str += '<tr>';
                str += '<td style="width: 45px">'+nums+'</td>';

                str += ctrlvGetXiaLaStrZuo(pub_budtype, 'budtype', list[key].budtype)

                str += '<td><input type="text" class="layui-input name" value="'+list[key].name+'"></td>';
                str += '<td><input type="text" class="layui-input unit" value="'+list[key].unit+'"></td>';
                str += '<td><input type="number" class="layui-input num" value="'+list[key].num+'"></td>';
                str += '<td><input type="number" class="layui-input price" value="'+list[key].price+'"></td>';
                str += '<td><input type="number" class="layui-input money disabled" disabled></td>';
                str += '<td><input type="text" class="layui-input explain" value="'+list[key].explain+'"></td>';
                str += '</tr>';
                $("#list_list").append(str);
            }
        }
        update_refresh_row_func();
        render_date();
        render_from();
    }
</script>
</body>
</html>
